package com.stedu.controller;

import cn.hutool.captcha.LineCaptcha;
import com.stedu.bean.Admin;
import com.stedu.bean.RespBean;
import com.stedu.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class AdminController {
    @Autowired
    public AdminService adminService;
    @ResponseBody
    //登录
    @PostMapping ("/login")
    public RespBean login(String username,String password,String codeInput,HttpSession session){
        //获取session保存的验证码
        String code = (String) session.getAttribute("code");
        if (code.equalsIgnoreCase(codeInput)){
            Admin login = adminService.login(username, password);
            if (login != null) {
                //将信息存放在session
                session.setAttribute("admin", login);
                return RespBean.ok("登陆成功");
            }else {
            return RespBean.error("请输入正确的信息");
            }
        }else {
            return RespBean.error("验证码错误");
        }
    }

    //注销
    @ResponseBody
    @GetMapping("/loginout")
    public RespBean loginout(HttpSession session){
        session.invalidate();
        return RespBean.ok("退出成功");
    }
    @ResponseBody
    //验证码
    @GetMapping("/code")
    public void code(HttpSession session, HttpServletResponse response) throws IOException {
        //生成验证码
        LineCaptcha lineCaptcha = new LineCaptcha(120, 30, 4, 10);
        //文本保存在session
        session.setAttribute("code", lineCaptcha.getCode());
        //验证吗的图片返回前端
        lineCaptcha.write(response.getOutputStream());
    }
    @ResponseBody
    @GetMapping("/admin/info")
    public RespBean info(HttpSession session) throws ServletException, IOException {

        Admin admin = (Admin) session.getAttribute("admin");
        if(admin!=null) {
            System.out.println(admin.getUsername());
            return RespBean.ok("", admin);
        }else {
            return RespBean.error("用户未登录");
        }

    }
    @ResponseBody
    @RequestMapping("/adminup")
    public RespBean update( HttpSession session,String password , String password1 ) throws ServletException, IOException {
            Admin admin =(Admin)session .getAttribute("admin");
        String username = admin.getUsername();
        String password2 = admin.getPassword();
        //String password = request.getParameter("password");
        //String password2 = request.getParameter("password1");
        System.out.println(password);
        System.out.println(admin);
        if (password.equalsIgnoreCase(password2)){
            if (password.equalsIgnoreCase(password1)){
                return RespBean.error("修改后的密码与当前密码重复，请重新输入");
            }else{

                    adminService.uppassword(username, password1);
                    return RespBean.ok("修改成功");

            }
        }else {
            return RespBean.error("当前密码输入错误，请重新输入");
        }}
}
